#encoding:utf-8
import os,serial,logger
from time import sleep
import sys,re,common,time
execfile('../import.cfg')
sendData = heart_data
port = tcp_longconnection_test
os.system('main_basic_cmd '+port+' 115200 tcpLongConnection.cfg log.xls 1')
global ser
try:
    print u'打开成功'
    ser = serial.Serial(port=port,baudrate=115200,timeout=1)
except:
    print u"请查看是否成功打开设备端口："+port
    common.quit()

def time_stamp():
    return time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) 

def sendCmd():
    ser.write("AT+CIPSEND\r\n")
    out_str = ''
    count = 0
    while(True):
        read_buf=ser.read(30)
        out_str = out_str + read_buf
        print out_str.replace("\r\n","  ")
        if out_str.find(">") != -1:
            break
        sleep(0.1)
    ser.write(sendData[0])
    out_str = ''
    while(True):
        read_buf=ser.read(30)
        out_str = out_str + read_buf
        count = count+1
        if out_str:
            print out_str.replace("\r\n","  ")
            if out_str.find(sendData[1]) != -1:
                return
            out_str = ""
        else:
            count = count + 1
        sleep(0.1)
        if count>=500:
            print 'command timeout'
            print out_str
            common.quit()

print 'start'
while True:
    count = 1
    for i in range(720):
        print 'test 10 period : '+str(count)+' time'
        sendCmd()
        count = count + 1
        sleep(10)
        
    count = 1
    for i in range(120):
        print 'test 1 min : '+str(count)+' time'
        sendCmd()
        count = count + 1
        sleep(60)
    count = 1
    for i in range(24):
        print 'test 5 min : '+str(count)+' time'
        sendCmd()
        count = count + 1
        sleep(300) 
